﻿# -*- encoding: utf-8 -*-
from django.contrib.syndication.feeds import Feed
from django.utils.feedgenerator import Atom1Feed

from biologeek.journal.models import Post
from biologeek.bistrot.models import Thought
#from biologeek.comments.models import Comment
from biologeek.tagging.models import Tag, TaggedItem

class RSSFeed(Feed):
    description = u'Dernières mises à jour du site de David Larlet : biologeek.com relatives aux %s'
    author_name = 'David Larlet'
    author_link = 'http://larlet.fr/'
    copyright   = 'Copyright (c) 2004-2007, David Larlet, Licence Art Libre'

    def get_object(self, bits):
        if len(bits) == 1:
            bit = bits[0]
            if bit == 'journal':
                self.title = 'Flux RSS des billets du journal du site biologeek.com'
                self.link = '/journal/'
                self.description = self.description % u'billets du journal'
                return Post.published.all()[:25]

            elif bit == 'bistrot':
                self.title = u'Flux RSS des pensées du bistrot du site biologeek.com'
                self.link = '/bistrot/'
                self.description = self.description % u'pensées du bistrot'
                return Thought.published.all()[:25]

            elif bit == 'commentaires':
                self.title = u'Flux RSS des commentaires du site biologeek.com'
                self.link = '/commentaires/'
                self.description = self.description % u'commentaires'
                return Comment.published.all()[:25]

            elif Tag.objects.get(name=bit):
                tag = Tag.objects.get(name=bit)
                self.title = 'Flux RSS du tag %s du site biologeek.com' % tag.name
                self.link = '/%s/' % tag.name
                self.description = self.description % u'billets comportant le tag %s' % tag.name
                return TaggedItem.objects.get_by_model(Post, Tag.objects.get(name=bit))[:25]

            else:
                raise ObjectDoesNotExist

        elif [Tag.objects.get(name=tag) for tag in bits]:
            tags = [Tag.objects.get(name=tag) for tag in bits]
            tag_names = ', '.join([tag.name for tag in tags])
            self.title = 'Flux RSS des tags %s du site biologeek.com' % tag_names
            self.link = '/%s/' % (','.join([tag.name for tag in tags]))
            self.description = self.description % u'billets comportant les tags %s' % tag_names
            return TaggedItem.objects.get_by_model(Post, tags)[:25]

        else:
            raise ObjectDoesNotExist

    def items(self, obj):
        return obj


class AtomFeed(RSSFeed):
    feed_type = Atom1Feed
    subtitle = RSSFeed.description
